package com.example.communist_app_api.controller.web;


import com.example.communist_app_api.db.pojoDto.CarouselMap;
import com.example.communist_app_api.service.ArticleService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.CarouselMapReq;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/CarouselMap/")
@Api("轮播图控制器")
public class CarouselMapController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    @PostMapping("updateCarouselMap")
    @ApiOperation("取消轮播图")
    public R updateCarouselMap(@RequestBody CarouselMapReq req){
        Integer id = req.getId();
        if (articleService.noCarouselMap(id)){
            return R.ok();
        }
        else return R.error();
    }

    @PostMapping("insertCarouselMap")
    @ApiOperation("添加轮播图")
    public R insertCarouselMap(@RequestBody CarouselMapReq req){
        Integer id = req.getId();
        if (articleService.isCarouselMap(id)){
            return R.ok();
        }
        else return R.error();
    }


    private Integer getUserIdByWeb(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String userName = claims.getSubject();
        Integer userIdByWeb = userService.getUserInfo(userName).getId();
        return userIdByWeb;
    }
}
